{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-2-013824e6803f>:2: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please write your own downloading logic.\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting ,\\train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting ,\\train-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.one_hot on tensors.\n",
      "Extracting ,\\t10k-images-idx3-ubyte.gz\n",
      "Extracting ,\\t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "Extracting ,\\train-images-idx3-ubyte.gz\n",
      "Extracting ,\\train-labels-idx1-ubyte.gz\n",
      "Extracting ,\\t10k-images-idx3-ubyte.gz\n",
      "Extracting ,\\t10k-labels-idx1-ubyte.gz\n"
     ]
    }
   ],
   "source": [
    "# 两种编码方式\n",
    "mnist = input_data.read_data_sets(',', one_hot=True)\n",
    "mnist2 = input_data.read_data_sets(',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((55000, 784), (55000, 784))"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mnist.train.images.shape,mnist2.train.images.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((55000, 10), (55000, 10))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mnist.train.labels.shape,mnist.train.labels.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor 'Placeholder:0' shape=<unknown> dtype=float32>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "learn_rate = tf.placeholder(tf.float32)\n",
    "learn_rate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-7-3a0315c4dd2a>:11: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "\n",
      "Future major versions of TensorFlow will allow gradients to flow\n",
      "into the labels input on backprop by default.\n",
      "\n",
      "See @{tf.nn.softmax_cross_entropy_with_logits_v2}.\n",
      "\n",
      "WARNING:tensorflow:From <ipython-input-7-3a0315c4dd2a>:16: arg_max (from tensorflow.python.ops.gen_math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use `argmax` instead\n"
     ]
    }
   ],
   "source": [
    "x = tf.placeholder(tf.float32,[None, 784], name='X')\n",
    "y = tf.placeholder(tf.float32, [None,10], name='y')\n",
    "\n",
    "\n",
    "W = tf.Variable(tf.truncated_normal([784,10]), name='Weight')\n",
    "b = tf.Variable(tf.zeros(10), name='bias')\n",
    "\n",
    "logits = tf.matmul(x,W) + b\n",
    "\n",
    "# softmax交叉熵,求LOSS\n",
    "cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=logits))\n",
    "\n",
    "# 梯度下降法，优化器\n",
    "train_step = tf.train.GradientDescentOptimizer(learn_rate).minimize(cross_entropy)\n",
    "# 判断是否相等\n",
    "correct_predict = tf.equal(tf.arg_max(y,1),tf.arg_max(logits,1))\n",
    "# tf.cast()函数的作用是执行 tensorflow 中张量数据类型转换\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_predict,tf.float32))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# tf.Session()创建一个会话，当上下文管理器退出时会话关闭和资源释放自动完成。\n",
    "sess = tf.Session()\n",
    "# tf.global_variables_initializer()添加节点用于初始化所有的变量\n",
    "sess.run(tf.global_variables_initializer())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "####################\n",
      "step [100], entropy loss: [1.0789836645126343]\n",
      "0.90625\n",
      "0.8007\n",
      "####################\n",
      "step [200], entropy loss: [0.11237235367298126]\n",
      "1.0\n",
      "0.8474\n",
      "####################\n",
      "step [300], entropy loss: [0.6387909650802612]\n",
      "0.90625\n",
      "0.8569\n",
      "####################\n",
      "step [400], entropy loss: [0.1337076723575592]\n",
      "1.0\n",
      "0.8539\n",
      "####################\n",
      "step [500], entropy loss: [0.4801744222640991]\n",
      "0.9375\n",
      "0.8712\n",
      "####################\n",
      "step [600], entropy loss: [0.13571122288703918]\n",
      "1.0\n",
      "0.8766\n",
      "####################\n",
      "step [700], entropy loss: [1.6897252798080444]\n",
      "0.875\n",
      "0.8749\n",
      "####################\n",
      "step [800], entropy loss: [1.3914659023284912]\n",
      "0.84375\n",
      "0.8802\n",
      "####################\n",
      "step [900], entropy loss: [1.027248501777649]\n",
      "0.96875\n",
      "0.8463\n",
      "####################\n",
      "step [1000], entropy loss: [0.45424702763557434]\n",
      "0.96875\n",
      "0.8871\n",
      "####################\n",
      "step [1100], entropy loss: [0.29426249861717224]\n",
      "1.0\n",
      "0.8801\n",
      "####################\n",
      "step [1200], entropy loss: [1.2874610424041748]\n",
      "0.875\n",
      "0.885\n",
      "####################\n",
      "step [1300], entropy loss: [0.036947187036275864]\n",
      "1.0\n",
      "0.8929\n",
      "####################\n",
      "step [1400], entropy loss: [0.23215238749980927]\n",
      "1.0\n",
      "0.8539\n",
      "####################\n",
      "step [1500], entropy loss: [0.3834170401096344]\n",
      "1.0\n",
      "0.8977\n",
      "####################\n",
      "step [1600], entropy loss: [0.33838915824890137]\n",
      "0.96875\n",
      "0.9009\n",
      "####################\n",
      "step [1700], entropy loss: [0.9695826768875122]\n",
      "0.9375\n",
      "0.8885\n",
      "####################\n",
      "step [1800], entropy loss: [1.3221344947814941]\n",
      "0.84375\n",
      "0.8878\n",
      "####################\n",
      "step [1900], entropy loss: [0.384755939245224]\n",
      "0.96875\n",
      "0.8635\n",
      "####################\n",
      "step [2000], entropy loss: [0.13669569790363312]\n",
      "1.0\n",
      "0.8943\n",
      "####################\n",
      "step [2100], entropy loss: [0.1815803349018097]\n",
      "0.96875\n",
      "0.9029\n",
      "####################\n",
      "step [2200], entropy loss: [0.05603519082069397]\n",
      "1.0\n",
      "0.9055\n",
      "####################\n",
      "step [2300], entropy loss: [0.08155953884124756]\n",
      "0.96875\n",
      "0.9115\n",
      "####################\n",
      "step [2400], entropy loss: [0.5272653102874756]\n",
      "0.90625\n",
      "0.9038\n",
      "####################\n",
      "step [2500], entropy loss: [1.0075758695602417]\n",
      "0.875\n",
      "0.9082\n",
      "####################\n",
      "step [2600], entropy loss: [0.8373053073883057]\n",
      "0.84375\n",
      "0.9073\n",
      "####################\n",
      "step [2700], entropy loss: [0.33640480041503906]\n",
      "0.96875\n",
      "0.909\n",
      "####################\n",
      "step [2800], entropy loss: [0.0462171733379364]\n",
      "1.0\n",
      "0.9048\n",
      "####################\n",
      "step [2900], entropy loss: [0.276817262172699]\n",
      "1.0\n",
      "0.9094\n",
      "####################\n",
      "step [3000], entropy loss: [0.12976400554180145]\n",
      "1.0\n",
      "0.9017\n",
      "####################\n",
      "step [3100], entropy loss: [0.5669043660163879]\n",
      "0.90625\n",
      "0.9072\n",
      "####################\n",
      "step [3200], entropy loss: [0.11620059609413147]\n",
      "0.96875\n",
      "0.9037\n",
      "####################\n",
      "step [3300], entropy loss: [0.06374341994524002]\n",
      "1.0\n",
      "0.9125\n",
      "####################\n",
      "step [3400], entropy loss: [0.6124732494354248]\n",
      "0.84375\n",
      "0.9112\n",
      "####################\n",
      "step [3500], entropy loss: [0.885595977306366]\n",
      "0.9375\n",
      "0.9058\n",
      "####################\n",
      "step [3600], entropy loss: [0.2891967296600342]\n",
      "0.96875\n",
      "0.9097\n",
      "####################\n",
      "step [3700], entropy loss: [0.29516005516052246]\n",
      "0.9375\n",
      "0.9047\n",
      "####################\n",
      "step [3800], entropy loss: [0.08925075829029083]\n",
      "1.0\n",
      "0.9077\n",
      "####################\n",
      "step [3900], entropy loss: [0.18309363722801208]\n",
      "1.0\n",
      "0.909\n",
      "####################\n",
      "step [4000], entropy loss: [0.34123268723487854]\n",
      "0.96875\n",
      "0.913\n",
      "####################\n",
      "step [4100], entropy loss: [0.2885678708553314]\n",
      "0.90625\n",
      "0.9144\n",
      "####################\n",
      "step [4200], entropy loss: [0.2761790156364441]\n",
      "0.96875\n",
      "0.9136\n",
      "####################\n",
      "step [4300], entropy loss: [0.21836718916893005]\n",
      "0.9375\n",
      "0.9134\n",
      "####################\n",
      "step [4400], entropy loss: [0.5715203881263733]\n",
      "0.90625\n",
      "0.9136\n",
      "####################\n",
      "step [4500], entropy loss: [0.2289143055677414]\n",
      "0.9375\n",
      "0.9126\n",
      "####################\n",
      "step [4600], entropy loss: [0.42938876152038574]\n",
      "0.84375\n",
      "0.9145\n",
      "####################\n",
      "step [4700], entropy loss: [0.8778678178787231]\n",
      "0.84375\n",
      "0.9143\n",
      "####################\n",
      "step [4800], entropy loss: [0.04635856673121452]\n",
      "1.0\n",
      "0.913\n",
      "####################\n",
      "step [4900], entropy loss: [0.1666976809501648]\n",
      "0.9375\n",
      "0.9137\n",
      "####################\n",
      "step [5000], entropy loss: [0.218024343252182]\n",
      "0.875\n",
      "0.911\n",
      "####################\n",
      "step [5100], entropy loss: [0.9637590646743774]\n",
      "0.78125\n",
      "0.915\n",
      "####################\n",
      "step [5200], entropy loss: [0.44367438554763794]\n",
      "0.90625\n",
      "0.9142\n",
      "####################\n",
      "step [5300], entropy loss: [0.25112947821617126]\n",
      "0.90625\n",
      "0.9148\n",
      "####################\n",
      "step [5400], entropy loss: [0.14705903828144073]\n",
      "0.96875\n",
      "0.912\n",
      "####################\n",
      "step [5500], entropy loss: [0.19111663103103638]\n",
      "0.9375\n",
      "0.9133\n",
      "####################\n",
      "step [5600], entropy loss: [0.3645288348197937]\n",
      "0.875\n",
      "0.9123\n",
      "####################\n",
      "step [5700], entropy loss: [0.06329728662967682]\n",
      "1.0\n",
      "0.9143\n",
      "####################\n",
      "step [5800], entropy loss: [0.5620008707046509]\n",
      "0.90625\n",
      "0.9139\n",
      "####################\n",
      "step [5900], entropy loss: [0.29348689317703247]\n",
      "0.96875\n",
      "0.9123\n",
      "####################\n",
      "step [6000], entropy loss: [0.34005534648895264]\n",
      "0.90625\n",
      "0.9146\n"
     ]
    }
   ],
   "source": [
    "for step in range(6000):\n",
    "    if step < 2000:\n",
    "        lr = 1.0\n",
    "    elif 2000 < step < 4000:\n",
    "        lr = 0.3\n",
    "    else:\n",
    "        lr = 0.1\n",
    "    batch_x, batch_y = mnist.train.next_batch(32)\n",
    "    _, loss = sess.run([train_step, cross_entropy],feed_dict={x: batch_x,y: batch_y,learn_rate: lr})\n",
    "    if (step + 1) % 100 == 0:\n",
    "        print('#' * 20)\n",
    "        print('step [{}], entropy loss: [{}]'.format(step + 1, loss))\n",
    "        print(sess.run(accuracy, feed_dict={x: batch_x, y: batch_y}))\n",
    "        print(sess.run(accuracy,feed_dict={x: mnist.test.images, y: mnist.test.labels}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
